/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.examples;

import jbeagle.core.*;
import jbeagle.core.select.TournamentSelector;
import jbeagle.core.ops.SimpleCrossover;
import jbeagle.core.ops.BinaryMutator;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;

public class OneMax {

	public static void main( String[] args ) {
		GAFactory gaf = new GAFactory("gaconfig.xml");
		
		GeneticAlgorithm<ListIndividual<Integer>, Integer> ga = gaf.create();
		
		System.out.println(ga.getPopulation());
		//init(ga);
		ga.run();
		System.out.println("========= FINAL POULATION ===========");
		Population pop = ga.getPopulation();
		System.out.println(pop);
		
		/*ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream( new FileOutputStream("population.ser") );
			oos.writeObject(pop);
		} catch (IOException e) {
			System.out.println("Oops, an IOException occurred.");
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				System.out.println("Oops, an IOException occurred.");
			}
		}
		/*List<double[]> resultsList = ga.getResults();
		double[][] results = resultsList.toArray(new double[2][100]);
		System.out.println(arrayToString(results, 2));*/
	}
	
	
	@SuppressWarnings("unchecked")
	public static void init( GeneticAlgorithm ga ) {
		//Set selection method and genetic operators
		ga.setSelectionMethod( new TournamentSelector(2) );
		ga.addOperator( new SimpleCrossover<ListIndividual<Integer>, Integer>() );
		ga.addOperator( new BinaryMutator() );
		
		//Create initial population
		IntegerIndividualFactory factory = new IntegerIndividualFactory(10, 10);
		ga.setPopulation( new Population<ListIndividual<Integer>, Integer>(60, factory) );
		
		ga.setFitnessFunction( new OneMaxFitnessFunction() );
		
		//Stop after 100 generations
		ga.setStopCondition( ga.new StopCondition(100) );
	}
	
	public static String arrayToString( double[][] A, int opt) {
		String s = "";
		int m = A.length;
		if ( opt == 1 )
			for ( int i = 0; i < m; i++ ) {
				for ( int j = 0; j < A[i].length; j++ )
					if (j == (A[i].length-1))
						s += A[i][j] + "\n";
					else
						s += A[i][j] + " ";
			}
		else if ( opt == 2 )
			for ( int j = 0; j < A[0].length; j++ )
				for ( int i = 0; i < m; i++ )
					if (i == (m-1))
						s += A[i][j] + "\n";
					else
						s += A[i][j] + " ";
		return s;
	}
}
